import { defineConfig, loadEnv, ConfigEnv, UserConfig } from "vite";
import { resolve } from "path";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import eslintPlugin from "vite-plugin-eslint";
import vueSetupExtend from "unplugin-vue-setup-extend-plus/vite";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
// https://devtools-next.vuejs.org/
import VueDevTools from "vite-plugin-vue-devtools";

// @see: https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  return {
    base: "/",
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src")
      }
    },
    define: {},
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/var.scss";`
        }
      }
    },
    server: {
      host: "0.0.0.0",
      port: 9000,
      open: true,
      cors: true,
      // Load proxy configuration from .env.development
      proxy: {
        "/api/": {
          target: `http://127.0.0.1:7645`,
          changeOrigin: true
        }
      }
    },
    plugins: [
      vue(),
      // vue 可以使用 jsx/tsx 语法
      vueJsx(),
      // esLint 报错信息显示在浏览器界面上
      eslintPlugin(),
      // name 可以写在 script 标签上
      vueSetupExtend({}),
      // 使用 svg 图标
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), "src/assets/icons")],
        symbolId: "icon-[dir]-[name]"
      }),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false // css in js
          })
        ],
        dirs: ["src/components"],
        dts: false
      }),
      VueDevTools({
        openInEditorHost: `http://localhost:9000`
      })
    ],
    // plugins: createVitePlugins(viteEnv),
    build: {
      outDir: "dist",
      minify: "esbuild",
      // esbuild 打包更快，但是不能去除 console.log，terser打包慢，但能去除 console.log
      // minify: "terser",
      // terserOptions: {
      // 	compress: {
      // 		drop_console: viteEnv.VITE_DROP_CONSOLE,
      // 		drop_debugger: true
      // 	}
      // },
      sourcemap: false,
      // 禁用 gzip 压缩大小报告，可略微减少打包时间
      reportCompressedSize: false,
      // 规定触发警告的 chunk 大小
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          // Static resource classification and packaging
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
        }
      }
    }
  };
});
